import os
import requests

#python -u "d:\nyahentai\author.py"

proxies = {
        "http": "http://127.0.0.1:10809",
        "https": "http://127.0.0.1:10809",
    }

def DownloadImages(author,doujinshi,images):
    if images:
        print(doujinshi)
        print(len(images))
        count = 0
        for image in images:
            print(image)
            response=requests.get(image)
            f3 = open("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\"+str(count).zfill(2)+".jpg","wb")
            f3.write(response.content)
            data = {
                'key': 'ponpomuyuri0987654322135Sfasfzs',
                'action': 'upload',
                'source': image,
                'format': 'txt'
            }
            r = requests.post("https://img.ponpomu.com/api/1/upload",data)
            f4 = open("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\CheveretoImageHosting.txt","a",encoding="utf-8")
            f4.write(r.text+"\n")
            f4.close()
            count += 1
        f4 = open("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\CheveretoImageHosting.txt","r",encoding="utf-8")
        PicUrls = f4.readlines()
        f4.close()
        f5 = open("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\"+doujinshi+".md","w",encoding="utf-8")
        f5.write("| 1 | 2 | 3 |\n| ------ | ------ | ------ |\n|")
        for i in range(((len(images)//3)+1)*3-len(images)):
            PicUrls.append("")
        for i in range(len(PicUrls)):
            f5.write("![thumb.jpg]("+PicUrls[i].strip("\n")+")|")
            if (i+1)%3==0:
                f5.write("\n|")
        f5.close()
f1 = open("D:\\nyahentai\\YuriAuthor.txt","r",encoding = "utf-8")
AuthorLines = f1.readlines()
f1.close()
f2 = open("D:\\nyahentai\\YuriAuthor2.txt","r",encoding = "utf-8")
lines = f2.readlines()
f2.close()
author = ""
doujinshi = ""
images = []
for line in lines:
    if line in AuthorLines:
        if (author != "") and (doujinshi != ""):
            DownloadImages(author,doujinshi,images)
        author = line.strip("\n")
        if os.path.exists("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\")==False:
            print("create D:\\nyahentai\\author\\"+author+"\\")
            os.mkdir("D:\\nyahentai\\author\\"+author+"\\")
        images = []
    elif ("https" not in line) and ("base64" not in line):
        if (author != "") and (doujinshi != ""):
            DownloadImages(author,doujinshi,images)
        doujinshi = line.strip("\n")
        images = []
        if os.path.exists("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\")==False:
            print("create "+"D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\")
            os.mkdir("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\")
            f4 = open("D:\\nyahentai\\author\\"+author+"\\"+doujinshi+"\\CheveretoImageHosting.txt","w",encoding="utf-8")
    elif "t1.mspcdn.xyz" in line:
        images.append(line.strip("\n"))
DownloadImages(author,doujinshi,images)